﻿-- =============================================
-- Author:		zengfei
-- Create date: 03m/17/2012
-- Description:	更新销售单主表
-- =============================================
CREATE PROCEDURE [dbo].[Usp_ModifySalesMaster]
	@p_Action	char(1),
	@p_SalesNo	varchar(30),
	@p_CustomerNo	varchar(8),
	@p_Dept	int,
	@p_ContractName	varchar(30),
	@p_ContractNo	varchar(30),
	@p_Warehouse	int,
	@p_Employee	varchar(20),
	@p_ReceiveAcccount	varchar(30),
	@p_ReceiveAmount	numeric(15,2),
	@p_ReceiveDate	datetime,
	@p_SalesDate	datetime,
	@p_EntryDate	datetime,
	@p_EntryEmployee	varchar(20),
	@p_InvoiceNo	varchar(30),
	@p_InvoiceDate	datetime,
	@p_ShipFormNo	varchar(30), 
	@p_ShipDate	datetime,
	@p_ReferenceNo	varchar(30), 
	@p_Remark	varchar(120),
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
begin transaction

if	@p_Action = 'A'
begin
	insert into SalesMaster (SalesNo, CustomerNo, Dept, ContractName, ContractNo, Warehouse, Employee, ReceiveAccount, ReceiveAmount, ReceiveDate, SalesDate, EntryDate, EntryEmployee, InvoiceNo, InvoiceDate, ShipFormNo, ShipDate, ReferenceNo, Remark)
		values (@p_SalesNo, @p_CustomerNo, @p_Dept, @p_ContractName, @p_ContractNo, @p_Warehouse, @p_Employee, @p_ReceiveAcccount, @p_ReceiveAmount, @p_ReceiveDate, @p_SalesDate, @p_EntryDate, @p_EntryEmployee, @p_InvoiceNo, @p_InvoiceDate, @p_ShipFormNo, @p_ShipDate, @p_ReferenceNo, @p_Remark)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	update SalesMaster set CustomerNo = @p_CustomerNo,
								Dept = @p_Dept,
								ContractName = @p_ContractName,
								ContractNo = @p_ContractNo,
								Warehouse = @p_Warehouse, 
								Employee = @p_Employee, 
								ReceiveAccount = @p_ReceiveAcccount, 
								ReceiveAmount = @p_ReceiveAmount, 
								ReceiveDate = @p_ReceiveDate, 
								SalesDate = @p_SalesDate, 
								EntryDate = @p_EntryDate, 
								EntryEmployee = @p_EntryEmployee, 
								InvoiceNo = @p_InvoiceNo, 
								InvoiceDate = @p_InvoiceDate, 
								ShipFormNo = @p_ShipFormNo, 
								ShipDate = @p_ShipDate, 
								ReferenceNo = @p_ReferenceNo, 
								Remark = @p_Remark 
							where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	delete from SalesMaster where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

insert into SalesMaster_log (SalesNo, CustomerNo, Dept, ContractName, ContractNo, Warehouse, Employee, ReceiveAccount, ReceiveAmount, ReceiveDate, SalesDate, EntryDate, EntryEmployee, InvoiceNo, InvoiceDate, ShipFormNo, ShipDate, ReferenceNo, Remark, RecordID, UserID, ModifyTime, [Action])
	values (@p_SalesNo, @p_CustomerNo, @p_Dept, @p_ContractName, @p_ContractNo, @p_Warehouse, @p_Employee, @p_ReceiveAcccount, @p_ReceiveAmount, @p_ReceiveDate, @p_SalesDate, @p_EntryDate, @p_EntryEmployee, @p_InvoiceNo, @p_InvoiceDate, @p_ShipFormNo, @p_ShipDate, @p_ReferenceNo, @p_Remark, @p_RecordID, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1

